import * as THREE from "three";

import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";
import { DRACOLoader } from "three/examples/jsm/loaders/DRACOLoader.js";
import { CSS3DObject } from "three/examples/jsm/renderers/CSS3DRenderer";
import $ from "jquery";
import { RGBELoader } from "three/examples/jsm/loaders/RGBELoader";

export function loaderModel(
  scene,
  css3dLabelGroup,
  aMeiGroup,
  bMeiGroup,
  cMeiGroup,
  dMeiGroup,
  eMeiGroup,
  xiaoMeiGroup
) {
  let url = "static/model/MeiCang_241216A.glb";
  const loader = new GLTFLoader();
  const dracoLoader = new DRACOLoader();
  dracoLoader.setDecoderPath("static/draco/");
  loader.setDRACOLoader(dracoLoader);
  //   const hdrEquirect = new RGBELoader()
  //     .setPath("static/hdr/")
  //     .load("autumn_field_puresky_2k.hdr", function (texture) {
  //       console.log(texture);
  //       hdrEquirect.mapping = THREE.EquirectangularReflectionMapping;
  //       ///scene.background = texture;
  //       scene.environment = texture;
  //       texture.hdrStrength = 0.8;
  //     });
  loader.load(url, (gltf) => {
    let gltfs = gltf.scene;
    gltfs.traverse(function (child) {
      let item = child.clone();
      if (item instanceof THREE.Mesh) {
        if (
          item.name == "Label_A" ||
          item.name == "Label_B" ||
          item.name == "Label_C" ||
          item.name == "Label_D" ||
          item.name == "Label_E"
        ) {
          // label标签
          let splitLabel = item.name.split("_")[1];
          let labelDiv = $(
            `<div id="${item.name}" class="model-label">
			    <p class="name">煤仓${splitLabel}</p>
			    <p class="weight">煤仓总煤量(t)：<span id="zongmeiliang_${splitLabel}">16</span></p>
			    <p class="thing">当前燃料</p>
			    <p class="time">剩余燃烧时间(min)：<span id="time_${splitLabel}">04:33</span></p>
			  </div>`
          );
          let labelObj = new CSS3DObject(labelDiv[0]);
          let labelMesh = new THREE.Object3D();
          labelMesh.position.set(item.position.x, 130, item.position.z);
          labelMesh.scale.set(0.28, 0.28, 0.28);
          item.visible = false;
          labelMesh.add(labelObj);
          css3dLabelGroup.add(labelMesh);
        } else if (item.name.indexOf("A_Mei") != -1) {
          aMeiGroup.add(item);
        } else if (item.name.indexOf("B_Mei") != -1) {
          bMeiGroup.add(item);
        } else if (item.name.indexOf("C_Mei") != -1) {
          cMeiGroup.add(item);
        } else if (item.name.indexOf("D_Mei") != -1) {
          dMeiGroup.add(item);
        } else if (item.name.indexOf("E_Mei") != -1) {
          eMeiGroup.add(item);
        } else if (item.name.indexOf("XiaoMeiCang_in") != -1) {
          xiaoMeiGroup.add(item);
        } else if (item.name.indexOf("MeiCang") != -1) {
          item.material.color = new THREE.Color(0xffffff);
          item.material.sheenRoughness = 0.5;
          item.material.transparent = true;
          item.material.opacity = 0.2;
          scene.add(item);
        } else {
          scene.add(item);
        }
      }
    });
  });
}
